package hyl.base.flow;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import hyl.core.MyFun;

public class Track {
	/** 未启用 */
	public static final char UNABLE = '0';
	/** 准备就绪 */
	public static final char READY = '1';
	/** 完成 */
	public static final char COMPLETE = '2';
	/** 归档 */
	public static final char FILE = '3';
	/**异常*/
	public static final char ERROR = '9';

	/**
	 * 当前流的流水号
	 */
	public String flowid;
	// 每个环节的id编号,每个环节只保留最新的操作,历史操作不保留
	public Map<String, Long> linkids = new HashMap<String, Long>();

	/**
	 * 当前流的会话
	 */
	private Map<String,Object> _attribute=new ConcurrentHashMap<String, Object>();
	// 流处理模型
	private Flow _flow;
	// 表示流的状态组,标记组
	private char[] _marks;

	public Track(Flow flow, String flowid) {
		super();
		this._flow = flow;
		if (MyFun.isEmpty(flowid)) {
			this.flowid = MyFun.getUUID();
		} else {
			this.flowid = flowid;
		}
		_marks = flow.getDefVals().toCharArray();
	}

	public char[] getAll() {
		return _marks;
	}

	public void setAll(String value) {
		_marks = value.toCharArray();

	}

	public void setMarks(Map<String, Character> values) {
		for (Entry<String, Character> a : values.entrySet()) {
			setMark(a.getKey(), a.getValue());
		}
	}

	public Character getMark(String field) {
		return _marks[_flow.getIndex(field)];
	}

	public void setMark(String field, Character c) {
		_marks[_flow.getIndex(field)] = c;
	}

	public String getFlowid() {
		return flowid;
	}

	public String getFlowName() {
		if (_flow == null)
			return null;
		return _flow._name;
	}

	public Object getAttribute(String key) {
	  return	_attribute.get(key);
	}
	public void setAttribute(String key,Object  value) {
		_attribute.put(key, value);
	}
	/* 预留未实现的方法 */
	public void finish() {
		if (_flow == null)
			return;
		_flow._pf.getFlowDao().finish(flowid);
	}

}
